package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjAdmOrderByField;
import com.zb.entity.ZjWorkOrderByField;
import com.zb.mapper.ZjWorkOrderByFieldMapper;
import com.zb.service.ZjWorkOrderByFieldService;
import com.zb.vo.OrderTableInitOrderByFieldVo;
import com.zb.vo.WorkInitOrderByFieldVo;
import com.zb.vo.WorkOrderByFieldVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 工单列表排序字段表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-01-24
 */
@Service
public class ZjWorkOrderByFieldServiceImpl extends ServiceImpl<ZjWorkOrderByFieldMapper, ZjWorkOrderByField> implements ZjWorkOrderByFieldService {
    @Resource
    private ZjWorkOrderByFieldMapper workOrderByFieldMapper;

    // 新增或修改工单列表排序字段
    @Override
    @Transactional
    public void save(AccounLoginInfo loginInfo, Integer type, List<WorkOrderByFieldVo> fieldVos) {
        Assert.notEmpty(fieldVos, "工单列表排序字段数据不能为空，至少填写一条");
        for (WorkOrderByFieldVo vo : fieldVos) {
            Assert.notNull(vo.getAdmOrderByFieldId(), "原始工单列表排序ID不能为空");
            Assert.notNull(vo.getOrderByField(), "排序字段不能为空");
            Assert.notNull(vo.getOrderBy(), "排序不能为空");
        }
        List<ZjAdmOrderByField> orderByFields = workOrderByFieldMapper.getInitOrderByField();
        List<String> admOrderByFieldIds = fieldVos.stream().map(vo -> vo.getAdmOrderByFieldId().toString())
                .filter(f -> orderByFields.stream().noneMatch(f2 -> Objects.equals(f2.getId().toString(), f))).toList();
        Assert.isTrue(CollectionUtil.isEmpty(admOrderByFieldIds), "原始工单列表排序ID数据不存在：" + String.join("、", admOrderByFieldIds));

        List<ZjWorkOrderByField> orderByFieldList = workOrderByFieldMapper.getOrderByFieldByCustAccIdAndAccountId(loginInfo.getCustAccId(), type, loginInfo.getId());
        List<ZjWorkOrderByField> adds = new ArrayList<>();
        List<ZjWorkOrderByField> updates = new ArrayList<>();
        ZjWorkOrderByField field = null;
        Long id = null;
        for (WorkOrderByFieldVo vo : fieldVos) {
            field = new ZjWorkOrderByField();
            BeanUtil.copyProperties(vo, field);
            if (null != field.getId()) {
                updates.add(field);
                continue;
            }
            id = orderByFieldList.stream().filter(f -> Objects.equals(f.getAdmOrderByFieldId(), vo.getAdmOrderByFieldId())).map(ZjWorkOrderByField::getId).findFirst().orElse(null);
            if (null != id) {
                field.setId(id);
                updates.add(field);
                continue;
            }
            field.setId(IdWorker.getId());
            field.setCustAccId(loginInfo.getCustAccId());
            field.setType(type);
            field.setAccountId(loginInfo.getId());
            adds.add(field);
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
    }

    // 获取工单列表排序初始化字字段
    @Override
    public List<OrderTableInitOrderByFieldVo> getOrderTableInitOrderByField() {
        List<ZjAdmOrderByField> orderByFields = workOrderByFieldMapper.getInitOrderByField();
        return orderByFields.stream().map(field -> {
            OrderTableInitOrderByFieldVo vo = new OrderTableInitOrderByFieldVo();
            BeanUtil.copyProperties(field, vo);
            return vo;
        }).toList();
    }

    // 获取工单列表排序字段设置
    @Override
    public List<WorkInitOrderByFieldVo> getOrderTableOrderByField(AccounLoginInfo loginInfo, Integer type) {
        List<ZjAdmOrderByField> orderByFields = workOrderByFieldMapper.getInitOrderByField();
        List<ZjWorkOrderByField> orderByFieldList = workOrderByFieldMapper.getOrderByFieldByCustAccIdAndAccountId(loginInfo.getCustAccId(), type, loginInfo.getId());
        return orderByFields.stream().filter(f -> !f.getDisabled()).map(admField -> {
            WorkInitOrderByFieldVo vo = new WorkInitOrderByFieldVo();
            BeanUtil.copyProperties(admField, vo);
            ZjWorkOrderByField field = orderByFieldList.stream().filter(f -> Objects.equals(f.getAdmOrderByFieldId(), admField.getId())).findFirst().orElse(null);
            vo.setAdmOrderByFieldId(admField.getId());
            if (null != field) {
                vo.setId(field.getId());
                vo.setOrderByField(field.getOrderByField());
                vo.setOrderBy(field.getOrderBy());
            } else {
                vo.setId(null);
            }
            return vo;
        }).toList();
    }

    // 根据商家账号信息 & 排序字段类型获取排序字段
    @Override
    public String getOrderTableOrderByFieldByCustAccIdAndAccountIdAndType(AccounLoginInfo loginInfo, Integer listType, Integer type) {
        String orderBy = "gmt_create DESC";
        Integer typeNum = 1; // 默认全部工单
        if (null != type) {
            if (type == 1) typeNum = 2;
            if (type == 2) typeNum = 3;
            if (type == 3) typeNum = 4;
            if (type == 1001) typeNum = 5;
            if (type == 4) typeNum = 6;
            if (type == 5) typeNum = 7;
        }
        ZjAdmOrderByField admOrderByField = workOrderByFieldMapper.getInitOrderByFieldByType(typeNum);
        if (null == admOrderByField) {
            return orderBy;
        }
        List<ZjWorkOrderByField> orderByFieldList = workOrderByFieldMapper.getOrderByFieldByCustAccIdAndAccountId(loginInfo.getCustAccId(), listType, loginInfo.getId());
        if (CollectionUtil.isEmpty(orderByFieldList) && null == admOrderByField) {
            return orderBy;
        }
        ZjWorkOrderByField orderByField = orderByFieldList.stream().filter(f -> Objects.equals(f.getAdmOrderByFieldId(), admOrderByField.getId())).findFirst().orElse(null);
        Integer field = null != orderByField? orderByField.getOrderByField() : admOrderByField.getOrderByField();
        Integer i = null != orderByField? orderByField.getOrderBy() : admOrderByField.getOrderBy();
        return (field == 1? "gmt_update" : field == 2? "gmt_create" : field == 3? "end_time" : "cancel_time") + (i == 1? " ASC" : " DESC");
    }
}
